package com.jandan.ui.client;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.jandan.ui.client.service.JandanContentService;
import com.jandan.ui.client.service.JandanContentServiceAsync;
import com.jandan.ui.model.Article;

public class JandanArticlesPanel extends Panel {
	public JandanArticlesPanel(){
		this.setTitle("煎蛋热文");
		createComponent();
	}
	
	private void createComponent(){
		JandanContentServiceAsync service = JandanContentService.Util.getInstance();
		service.getLatestJandanArticleList(new AsyncCallback<List<Article>>(){
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				MessageBox.alert("获取文章数据错误");
			}
			public void onSuccess(List<Article> result) {
				// TODO Auto-generated method stub
				display(result);
			}
		});
	}
	
	private void display(List<Article> articleList){
		Iterator<Article> it = articleList.iterator();
		StringBuffer sb = new StringBuffer("<div><ul>");
		
		while(it.hasNext()){
			Article article = it.next();
			String link = article.getLink();
			String title = article.getTitle();
			sb.append("<li><a href='");
			sb.append(link);
			sb.append("' target='_blank'>");
			sb.append(title);
			sb.append("</a></li>");
		}
		sb.append("</ul></div>");
		this.setHtml(sb.toString());
	}
}
